package com.base.myproject.client.busi;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.Scroll;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.EditorGrid;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.google.gwt.http.client.RequestException;

public class TestBusi2 extends BaseJDBCPanel {
	public TestBusi2()
	{
		super();
		//  this.setSize(800, height)
	}
	public void init()
	{
		super.init();
	}

	 public ContentPanel initWeiget() {  
		    setLayout(new FlowLayout(10));  
		  
		    CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		    List<ColumnConfig> columns = new ArrayList<ColumnConfig>();  
		    columns.add(cbs.getColumn());
		    ColumnConfig column = new ColumnConfig("Sender", "gcode2", 100); 
		    cbs.setSelectionMode(Style.SelectionMode.MULTI);
		    
		    
		    TextField<String> text = new TextField<String>(); 
		    column.setEditor(new CellEditor(text));
		  
		    columns.add(column);
		    
		    ColumnConfig gname = new ColumnConfig("gname", "Email", 100); 
		    
		    
		    TextField<String> gnametext = new TextField<String>(); 
		    gname.setEditor(new CellEditor(gnametext));
		    columns.add(gname);
		    //columns.add(new ColumnConfig("Sender", "gcode2", 100));  
		   // columns.add(new ColumnConfig("gname", "Email", 165));  
		    columns.add(new ColumnConfig("Phone", "Phone", 100));  
		    columns.add(new ColumnConfig("State", "State", 50));  
		    columns.add(new ColumnConfig("Zip", "Zip Code", 65));  
		  
		    // create the column model  
		    ColumnModel cm = new ColumnModel(columns);  
		  
		    // defines the xml structure  
		    ModelType type = new ModelType();  
		    type.setRoot("records");  
		    type.setRecordName("record");  
		    type.addField("Sender", "gcode");  
		    type.addField("gname");  
		    type.addField("Phone");  
		    type.addField("State");  
		    type.addField("Zip");  
		  
		    // use a http proxy to get the data  
		   /* RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, "TestServlet?name=ee");  
		    HttpProxy proxy = new HttpProxy(builder);  
		  
		    // need a loader, proxy, and reader  
		    XmlReader reader = new XmlReader(type);  
		  
		    final BaseListLoader loader = new BaseListLoader(proxy, reader);  
		  */
		   // final BaseStore<JDBCModelData> store = new BaseStore<JDBCModelData>(loader);
		    StoreInfo info1= new StoreInfo();
		    info1.setTablename("gcode");
		    info1.setSql("select * from gcode");
		    final BaseStore<JDBCModelData> store = new BaseStore<JDBCModelData>(type,info1);
		    store.setName("parent");
//		    store.addBaseStoreListener(1, new BaseStoreListener(){
//
//				@Override
//				public void rowChanged(BaseStoreEvent evt) {
//					// TODO Auto-generated method stub
//					System.out.println("parent out listener");
//				}
//
//				@Override
//				public void onBrowserEvent(Event event) {
//					// TODO Auto-generated method stub
//					
//				}
//		    	
//		    });
		    
		    final  EditorGrid grid=new EditorGrid(store, cm);
		    //store.getAt(index)
		    grid.setSelectionModel(cbs);
		    grid.setBorders(true);  
		    grid.setAutoExpandColumn("Sender");  
		  
		    ContentPanel panel = new ContentPanel();  
		    panel.setFrame(true);  
		    panel.setCollapsible(true);  
		    panel.setAnimCollapse(false);  
		    panel.setScrollMode(Scroll.ALWAYS);
		    panel.setButtonAlign(HorizontalAlignment.CENTER);  
		    panel.setIconStyle("icon-table");  
		    panel.setHeading("XML Table Demo");  
		  //  panel.setLayout(new FlowLayout());  
		    panel.setLayout(new FitLayout()); 
		    panel.add(grid);  
		    panel.setSize(575, 350);  
		  
		    // add buttons  
		    Button load = new Button("Load XML");  
		   // loader.setReuseLoadConfig(true);
		  //  boolean b= loader.load();  
		   
		    
//		    loader.addLoadListener(new LoadListener() { 
//				public void loaderBeforeLoad(LoadEvent le) { 
//				Info.display("JData","Staring Loading...."); 
//				//Window.alert("before load"); 
//				} 
//				public void loaderLoad(LoadEvent le) { 
//				Info.display("JData","Loading complete!"); 
//				
//				} 
//				 
//				public void loaderLoadException(LoadEvent le) { 
//				Info.display("JData","Loading Error!"+le.exception); 
//				System.out.println(le.exception);
//				
//				} 
//				}); 
		    panel.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	       //  grid.getStore().rejectChanges();  
		    	    	   store.rejectChanges();  
		    	       }  
		    	     }));
		    panel.addButton(new Button("delete", new SelectionListener<ButtonEvent>() {  
	    	       @Override  
	    	       public void componentSelected(ButtonEvent ce) {  
	    	       //  grid.getStore().rejectChanges();  
	    	    	   System.out.println(grid.getSelectionModel().getSelectedItem());
	    	    	   store.remove((JDBCModelData)grid.getSelectionModel().getSelectedItem());
	    	    	 //  System.out.println(store.indexOf(grid.getSelectionModel().getSelectedItem()));
	    	       }  
	    	     }));
		    panel.addButton(new Button("deleteall", new SelectionListener<ButtonEvent>() {  
	    	       @Override  
	    	       public void componentSelected(ButtonEvent ce) {  
	    	    	   store.removeAll();
	    	       //  grid.getStore().rejectChanges();  
//	    	    	   System.out.println(grid.getSelectionModel().getSelectedItem());
//	    	    	   store.remove(grid.getSelectionModel().getSelectedItem());
//	    	    	   System.out.println(store.indexOf(grid.getSelectionModel().getSelectedItem()));
	    	       }  
	    	     }));
		    panel.addButton(new Button("servletsender", new SelectionListener<ButtonEvent>() {  
	    	       @Override  
	    	       public void componentSelected(ButtonEvent ce) {  
	    	    	   try {
						store.sendServlet("SAVE", "BaseServlet", "name=aaa&age=13");
					} catch (RequestException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	    	       //  grid.getStore().rejectChanges();  
//	    	    	   System.out.println(grid.getSelectionModel().getSelectedItem());
//	    	    	   store.remove(grid.getSelectionModel().getSelectedItem());
//	    	    	   System.out.println(store.indexOf(grid.getSelectionModel().getSelectedItem()));
	    	       }  
	    	     }));
		    panel.addButton(new Button("add", new SelectionListener<ButtonEvent>() {  
	    	       @Override  
	    	       public void componentSelected(ButtonEvent ce) {  
	    	       //  grid.getStore().rejectChanges();  
	    	    	   HashMap hm = new HashMap();
	    	    	   hm.put("Sender", "zgw1");
	    	    	   hm.put("gname", "gname");
	    	    	   hm.put("Phone", "Phone");
	    	    	   JDBCModelData jdbcml = new JDBCModelData(hm);
	    	    	   store.add(jdbcml);
	    	       }  
	    	     }));
		    panel.addButton(new Button("Commit parent", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	    	  // System.out.println("beforesave="+grid.getStore().getAt(0).get("Sender"));
		    	    	//   grid.getStore().removeAll();
		    	        // grid.getStore().commitChanges(); 
		    	    	  // store.gotorow(1);
		    	    	   store.trySave();
		    	    	   System.out.println("move...parent");
		    	 
		    	    	 //  grid.getStore().remove(grid.getSelectionModel().getSelectedItem());
		    	      //  System.out.println("aftersave="+grid.getStore().getAt(0).get("Sender"));
		    	       }  
		    	    }));  
		    
		    //-------------------------2
		    CheckBoxSelectionModel cbs2 = new CheckBoxSelectionModel();
		    List<ColumnConfig> columns2 = new ArrayList<ColumnConfig>();  
		    columns2.add(cbs2.getColumn());
		    ColumnConfig column2 = new ColumnConfig("Sender", "gcode2", 100); 
		    cbs2.setSelectionMode(Style.SelectionMode.MULTI);
		    
		    
		    TextField<String> text2 = new TextField<String>(); 
		    column2.setEditor(new CellEditor(text2));
		  
		    columns2.add(column2);
		    //columns.add(new ColumnConfig("Sender", "gcode2", 100));  
		    columns2.add(new ColumnConfig("gname", "Email", 165));  
		    columns2.add(new ColumnConfig("Phone", "Phone", 100));  
		    columns2.add(new ColumnConfig("State", "State", 50));  
		    columns2.add(new ColumnConfig("Zip", "Zip Code", 65));  
		  
		    // create the column model  
		    ColumnModel cm2 = new ColumnModel(columns2);  
		  
		    // defines the xml structure  
		    ModelType type2 = new ModelType();  
		    type2.setRoot("records");  
		    type2.setRecordName("record");  
		    type2.addField("Sender", "gcode");  
		    type2.addField("gname");  
		    type2.addField("Phone");  
		    type2.addField("State");  
		    type2.addField("Zip");  
		  
		    // use a http proxy to get the data  
		   /* RequestBuilder builder2 = new RequestBuilder(RequestBuilder.POST, "TestServlet?name=ee");  
		    HttpProxy proxy2 = new HttpProxy(builder2);  
		  
		    // need a loader, proxy, and reader  
		    XmlReader reader2 = new XmlReader(type2);  
		  
		    final BaseListLoader loader2 = new BaseListLoader(proxy2, reader2);  
		  */
		  //  final BaseStore store2 = new BaseStore(loader2);  
		    StoreInfo info2= new StoreInfo();
		    info2.setTablename("gcode");
		    info2.setSql("select * from gcode");
		    final BaseStore store2 = new BaseStore(type2,info2);
		    store2.setName("chile");
		
		    final  BaseGrid grid2=new BaseGrid(store2, cm2);
		    //store.getAt(index)
		    grid2.setSelectionModel(cbs2);
		    grid2.setBorders(true);  
		    grid2.setAutoExpandColumn("Sender");  
		  
		    ContentPanel panel2 = new ContentPanel();  
		    panel2.setFrame(true);  
		    panel2.setCollapsible(true);  
		    panel2.setAnimCollapse(true);  
		    panel2.setScrollMode(Scroll.ALWAYS);
		    panel2.setButtonAlign(HorizontalAlignment.CENTER);  
		    panel2.setIconStyle("icon-table");  
		    panel2.setHeading("XML Table Demo2");  
		   // panel2.setLayout(new FlowLayout());  
		    panel2.setLayout(new FitLayout()); 
		    panel2.add(grid2);  
		    panel2.setSize(575, 350);  
		  
		    // add buttons  
		    Button load2 = new Button("Load XML");  
		   // loader.setReuseLoadConfig(true);
		   // boolean b2= loader2.load();  
	
		    
//		    loader2.addLoadListener(new LoadListener() { 
//				public void loaderBeforeLoad(LoadEvent le) { 
//				Info.display("JData","Staring Loading...."); 
//				//Window.alert("before load"); 
//				} 
//				public void loaderLoad(LoadEvent le) { 
//				Info.display("JData","Loading complete!"); 
//				
//				} 
//				 
//				public void loaderLoadException(LoadEvent le) { 
//				Info.display("JData","Loading Error!"+le.exception); 
//				System.out.println(le.exception);
//				
//				} 
//				}); 
		    panel2.addButton(new Button("Reset", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	         grid2.getStore().rejectChanges();  
		    	       }  
		    	     }));
		    panel2.addButton(new Button("Commit chile", new SelectionListener<ButtonEvent>() {  
		    	       @Override  
		    	       public void componentSelected(ButtonEvent ce) {  
		    	    	  // System.out.println("beforesave="+grid2.getStore().getModels().get(0)getAt(0).get("Sender"));
		    	    	  // System.out.println("beforesave="+grid2.getStore().getAt(0).get("Sender"));
		    	    	
		    	    	  // System.out.println("beforesave="+grid2.getStore().getRecord(grid2.getStore().getAt(0)).getModel().get("Sender"));
		    	    	   //System.out.println("beforesave="+((Record) grid2.getStore().getModifiedRecords().get(0)).getChanges().get("Sender"));
		    	    	  // System.out.println("beforesave="+ grid2.getStore().getRecord(((Record) grid2.getStore().getModifiedRecords().get(0)).getModel()).get("Sender"));
		    	    	   System.out.println(grid2.getSelectionModel().getSelectedItem());
		    	    	   System.out.println( grid2.getStore().indexOf(grid2.getSelectionModel().getSelectedItem()));
		    	    	   System.out.println(((BaseStore)grid2.getStore()).getValue(0, "Sender"));
		    	    	   System.out.println(((BaseStore)grid2.getStore()).getValueBefoEdited(0, "Sender"));
		    	    	  
		    	    	   System.out.println(grid2.getValue("Sender"));
		    	    	   grid2.setValue(2, "Sender", "zzz");
		    	    	   //   grid.getStore().removeAll();
		    	        // grid.getStore().commitChanges(); 
		    	    	   store2.gotorow(1);
		    	    	   System.out.println("move...chile");
		    	    	  // grid2.getStore().remove(grid.getSelectionModel().getSelectedItem());
		    	       
		    	       }  
		    	    }));  
		  
		    //-------------------------2 end
		    panel.addButton(load);  
		  //  add(panel);  
		
		   panel.add(panel2);
		   this.registerStore(store);
		   this.registerStore(store2);
		  return panel;
		  }  
	private void loadjdbc()
	{
		this.setTablename("");
		this.setSql(" select * from ");
	}
}
